<?php

namespace App\Http\Validate\Kms\Clients;

use App\Http\Validate\AbstractBaseValidate;
use App\Models\Kms\App\ClientsModel;
use App\Models\Kms\Fs\FsDepartmentsModel;
use App\Models\Kms\User\KmsUser;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ClientsValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function backendListValidate(array $params)
    {
        return self::getInstance()->validate(["pageNum", "pageSize"], $params, [
            "pageNum"  => "int|min:1",
            "pageSize" => "int|min:1"
        ]);
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function addValidate(array $params)
    {
        return self::getInstance()->validate(
            ["icon", "name", "desc", "authType", "uniAppId", "sort", "authUser", "authDepartment"],
            $params,
            [
                "icon"             => "required|url",
                "name"             => [
                    "required",
                    "string",
                    "min:1",
                    Rule::unique((new ClientsModel())->getTable(), "name")
                ],
                "desc"             => "required|string|min:1",
                "authType"         => self::intInValidateBuild(true, ClientsModel::AUTH_TYPE_TEXT),
                "uniAppId"         => [
                    "required",
                    "string",
                    "min:1",
                    Rule::unique((new ClientsModel())->getTable(), "uniAppId")
                ],
                "sort"             => "required|int|min:0",
                "authUser"         => [
                    Rule::requiredIf(function () use ($params) {
                        $authType       = attributeGet($params, "authType", null);
                        $authDepartment = attributeGet($params, "authDepartment", null);
                        return $authType == ClientsModel::AUTH_TYPE_PART && empty($authDepartment);
                    }),
                    "array"
                ],
                "authUser.*"       => [
                    "string",
                    "min:1",
                    Rule::exists((new KmsUser())->getTable(), "userId")
                ],
                "authDepartment"   => [
                    Rule::requiredIf(function () use ($params) {
                        $authType = attributeGet($params, "authType", null);
                        $authUser = attributeGet($params, "authUser", null);
                        return $authType == ClientsModel::AUTH_TYPE_PART && empty($authUser);
                    }),
                    "array"
                ],
                "authDepartment.*" => [
                    "string",
                    "min:1",
                    Rule::exists((new FsDepartmentsModel())->getTable(), "openDepartmentId")
                ]
            ]);
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function updateValidate(array $params)
    {
        return self::getInstance()->validate(
            ["id", "icon", "name", "desc", "authType", "uniAppId", "sort", "authUser", "authDepartment"],
            $params,
            [
                "id"               => [
                    "required",
                    "int",
                    Rule::exists((new ClientsModel())->getTable(), "id")
//                        ->where("type", ClientsModel::TYPE_APP)
                        ->where("isDeleted", 0)
                ],
                "icon"             => "required|url",
                "name"             => [
                    "required",
                    "string",
                    "min:1",
                    Rule::unique((new ClientsModel())->getTable(), "name")->where(function ($query) use ($params) {
                        $query->where("name", "<>", $params["name"]);
                    })
                ],
                "desc"             => "required|string|min:1",
                "authType"         => self::intInValidateBuild(true, ClientsModel::AUTH_TYPE_TEXT),
                "uniAppId"         => [
                    "exclude_unless:type," . ClientsModel::TYPE_APP,
                    "required",
                    "string",
                    "min:1",
                    Rule::unique((new ClientsModel())->getTable(), "uniAppId")->where(function ($query) use ($params) {
                        $query->where("id", "<>", $params["id"]);
                    })
                ],
                "sort"             => "required|int|min:0",
                "authUser"         => [
                    Rule::requiredIf(function () use ($params) {
                        $authType       = attributeGet($params, "authType", null);
                        $authDepartment = attributeGet($params, "authDepartment", null);
                        return $authType == ClientsModel::AUTH_TYPE_PART && empty($authDepartment);
                    }),
                    "array"
                ],
                "authUser.*"       => [
                    "string",
                    "min:1",
                    Rule::exists((new KmsUser())->getTable(), "userId")
                ],
                "authDepartment"   => [
                    Rule::requiredIf(function () use ($params) {
                        $authType = attributeGet($params, "authType", null);
                        $authUser = attributeGet($params, "authUser", null);
                        return $authType == ClientsModel::AUTH_TYPE_PART && empty($authUser);
                    }),
                    "array"
                ],
                "authDepartment.*" => [
                    "string",
                    "min:1",
                    Rule::exists((new FsDepartmentsModel())->getTable(), "openDepartmentId")
                ]
            ]);
    }


    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function statusValidate(array $params)
    {
        return self::getInstance()->validate(["id", "status"], $params, [
            "id"     => [
                "required",
                "int",
                Rule::exists((new ClientsModel())->getTable(), "id")
                    ->where("isDeleted", 0)
                    ->where("type", ClientsModel::TYPE_APP)
            ],
            "status" => self::intInValidateBuild(true, ClientsModel::STATUS_TEXT)
        ]);
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function deleteValidate(array $params)
    {
        return self::getInstance()->validate(["id"], $params, [
            "id" => [
                "required",
                "int",
                Rule::exists((new ClientsModel())->getTable(), "id")
                    ->where("isDeleted", 0)
                    ->where("type", ClientsModel::TYPE_APP)
            ]
        ]);
    }
}
